#pragma once

#include <vector>
#include <queue>
#include "SFML/Audio/SoundStream.hpp"
#include "SFML/System.hpp"

/*
Interface to receive notification when the player thread has nothing more to play
*/
class PlayerThreadListener
{
public:
	PlayerThreadListener(){}
	~PlayerThreadListener(){}

	//Notify that there's nothing more to play
	virtual void OnStreamEnded() = 0;
};

/*
Extension of SFML's soundplaying API. Runs in a separate thread.
*/
class PlayerThread : public sf::SoundStream
{
public:
	PlayerThread(void);
	~PlayerThread(void);

	void Initialize(PlayerThreadListener* listener, unsigned int samplingRate, unsigned int windowLength);

	//SoundStream overriding
	bool OnStart(){return true;}

	//SoundStream overriding - this is what eventually supplies the soundcard with audio data
	bool OnGetData(Chunk& Data);

	//Add data to PlayerThread's buffer
	void AddSamples(short* samples, unsigned int length);

	//Purge the PlayerThread's buffer
	void Purge();

	volatile bool isPlaying;
	PlayerThreadListener* listener;

private:
	sf::Mutex mutex;
	unsigned int windowLength;
	std::queue<short> myBuf;
	std::vector<short> lastOutput;
};
